package com.pig4cloud.pig.admin.task;

import cn.hutool.core.date.DateUtil;
import com.pig4cloud.pig.admin.api.entity.NewsList;
import com.pig4cloud.pig.admin.constant.CommonConstant;
import com.pig4cloud.pig.admin.task.base.BaseTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadLocalRandom;


@Component
public class CaijingNews extends BaseTask {

	private String websiteUrl = "http://app.finance.china.com.cn/news/my.php?cname=%E8%B4%A2%E7%BB%8F&p=";


	@Scheduled(cron = "0 0/5 * * * ?")
	public void doTask(){
		int randomPage = ThreadLocalRandom.current().nextInt(1, 551);
		String request = httpUtilService.getRequest(1,this.getClass(),websiteUrl+randomPage, CommonConstant.UTF8);
		Document parse = Jsoup.parse(request);
		Elements lis = parse.body().select(".news_list").select("li");
		//List<EsHotNews> commonDataList = new CopyOnWriteArrayList<>();
		for (Element li : lis){
			String time = li.select("span").text();
			String topic = li.select("a:nth-child(2)").text()+"-"+li.select("a:nth-child(3)").text();
			String link = li.select("a:nth-child(3)").attr("href");
			NewsList news = NewsList.create(idGenerator.nextId());
			news.setTitle(topic);
			news.setCreateTime(DateUtil.parse(time,"yyyy-MM-dd HH:mm"));
			news.setUrl(link);
			news.setCategory("财经");
			pushNewsToRedis(news);
		}
	}









}
